package com.example.springboot.service.impl;

import com.example.springboot.event.CompanyApplySealConsumer;
import com.example.springboot.event.CompanyApplySealEventPublisher;
import com.example.springboot.mapper.TeacherMapper;
import com.example.springboot.model.Teacher;
import com.example.springboot.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    @Lazy
    private TeacherService teacherService;
    @Autowired
    private CompanyApplySealEventPublisher publisher;

    @Transactional(rollbackFor = Exception.class)
    public void test1() {
        long start = System.currentTimeMillis();
        log.info("service线程：" + Thread.currentThread().getName());
        log.info("开始推送：" + start);
        CompanyApplySealConsumer companyApplySealConsumer = new CompanyApplySealConsumer();
        companyApplySealConsumer.setSealMakeStatus("1");
        companyApplySealConsumer.setIpAddr("192.168.0.1");
        publisher.publishEvent(companyApplySealConsumer);
        CompanyApplySealConsumer companyApplySealConsumer1 = new CompanyApplySealConsumer();
        companyApplySealConsumer1.setSealMakeStatus("2");
        companyApplySealConsumer1.setIpAddr("192.168.0.2");
        publisher.publishEvent(companyApplySealConsumer1);
        CompanyApplySealConsumer companyApplySealConsumer2 = new CompanyApplySealConsumer();
        companyApplySealConsumer2.setSealMakeStatus("3");
        companyApplySealConsumer2.setIpAddr("192.168.0.3");
        publisher.publishEvent(companyApplySealConsumer2);
        long end = System.currentTimeMillis();
        log.info("推送结束：" + end);
        log.info("推送耗时：" + (end - start));
    }

    @Transactional(rollbackFor = Exception.class)
    public void test() {
        long start = System.currentTimeMillis();
        log.info("开始时间外：" + start);
        log.info("当前线程外：" + Thread.currentThread().getName());

        Teacher teacher = new Teacher();
        teacher.setTeacherName("熊磊");
        teacherMapper.insert(teacher);
        teacherService.save("1");
        long end = System.currentTimeMillis();
        log.info("结束时间外：" + end);
        log.info("共耗时外：" + (end - start));
        // System.out.println(1/0);
    }

    /**
     * 开启异步
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void save(String status) {
        long start = System.currentTimeMillis();
        //log.info("开始时间：" + start);
        //log.info("当前线程：" + Thread.currentThread().getName());

        Teacher teacher = new Teacher();
        teacher.setTeacherName("熊莉" + status);
        // 先新增
        this.insertSelective(teacher);
        // 再删除
        this.deleteByPrimaryKey(15);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        //log.info("结束时间：" + end);
        //log.info("共耗时：" + (end - start));

    }
    /**
     * deleteByPrimaryKey
     * @param id id
     * @return int int
     */
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return teacherMapper.deleteByPrimaryKey(id);
    }

    /**
     * insert
     * @param record record
     * @return int int
     */
    @Override
    public int insert(Teacher record) {
        return teacherMapper.insert(record);
    }

    /**
     * insertSelective
     * @param record record
     * @return int int
     */
    @Override
    public int insertSelective(Teacher record) {
        return teacherMapper.insertSelective(record);
    }

    /**
     * selectByPrimaryKey
     * @param id id
     * @return TTeacher TTeacher
     */
    @Override
    public Teacher selectByPrimaryKey(Integer id) {
        return teacherMapper.selectByPrimaryKey(id);
    }

    /**
     * updateByPrimaryKeySelective
     * @param record record
     * @return int int
     */
    @Override
    public int updateByPrimaryKeySelective(Teacher record) {
        return teacherMapper.updateByPrimaryKeySelective(record);
    }

    /**
     * updateByPrimaryKey
     * @param record record
     * @return int int
     */
    @Override
    public int updateByPrimaryKey(Teacher record) {
        return teacherMapper.updateByPrimaryKey(record);
    }
}